package com.nifostasky.comic.controller;

import com.nifostasky.comic.entitys.SysMenu;
import com.nifostasky.comic.entitys.SysRole;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.service.SysRoleService;
import com.nifostasky.comic.utils.PageUtils;
import com.nifostasky.comic.vos.JsonResult;
import com.nifostasky.comic.vos.RoleVO;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysRole")
@Api(tags = "角色管理", position = 3)
@AllArgsConstructor
public class SysRoleController {

    private SysRoleService sysRoleService;

    @ApiOperation(value = "分页列表",notes = "带参分页查询列表")
    @GetMapping("findListPage")
    @ApiOperationSupport(order = 1)
    public JsonResult findListPage(PageUtils pageUtils, SysRole sysRole){
        return new JsonResult(sysRoleService.findListPage(pageUtils, sysRole));
    }

    @ApiOperation("新增/修改")
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 2)
    public JsonResult saveOrUpdate(@RequestBody SysRole role){
        return new JsonResult(sysRoleService.saveOrUpdate(role));
    }

    @ApiOperation("根据主键id获取信息")
    @GetMapping("/getData")
    @ApiResponses(@ApiResponse(code = 200,message = "返回成功",response = SysMenu.class))
    @ApiOperationSupport(order = 3)
    public JsonResult<SysRole> getData(@ApiParam(value = "主键id",required = true) Long id){
        return new JsonResult(sysRoleService.getById(id));
    }
    @ApiOperation("单个或批量删除")
    @GetMapping("/deleteByIds")
    @ApiOperationSupport(order = 4)
    public JsonResult deleteByIds(@ApiParam("主键ids，批量删除时id用英文逗号隔开数据格式:id1,id2,id3,...") String ids){
        if(StringUtils.isBlank(ids)){
            throw new BusinessException("缺少必填参数");
        }
        return new JsonResult(sysRoleService.deleteRoleAllById(ids));
    }

    @ApiOperation("角色设置菜单")
    @PostMapping("/setRoleMenu")
    @ApiOperationSupport(order = 5)
    public JsonResult setRoleMenu(@RequestBody RoleVO roleVO){
        Boolean bool = sysRoleService.setRoleMenu(roleVO.getId(),roleVO.getMenuIds());
        return new JsonResult(bool);
    }

    @ApiOperation(value = "根据角色ID查询菜单列表", notes = "根据角色ID查询菜单")
    @GetMapping("/getMenusByRole")
    @ApiOperationSupport(order = 6)
    public JsonResult getMenusByRole(@ApiParam(value = "角色ID", required = true) @RequestParam Long roleId){
        return new JsonResult<>(sysRoleService.getMenusByRole(roleId));
    }
    @ApiOperation(value = "获取角色菜单权限", notes = "根据角色ID查询菜单关联信息")
    @GetMapping("/getRoleMenuByRoleId")
    @ApiOperationSupport(order = 7)
    public JsonResult getRoleMenuByRoleId(@ApiParam(value = "角色ID", required = true) @RequestParam Long roleId){
        return new JsonResult<>(sysRoleService.getRoleMenuByRoleId(roleId));
    }
}
